class StockLoggingsController < ApplicationController
  before_action :set_stock_logging, only: [:show, :edit, :update, :destroy]

  # GET /stock_loggings
  # GET /stock_loggings.json
  def index
    @stock_loggings = StockLogging.all
  end

  # GET /stock_loggings/1
  # GET /stock_loggings/1.json
  def show
  end

  # GET /stock_loggings/new
  def new
    @stock_logging = StockLogging.new
  end

  # GET /stock_loggings/1/edit
  def edit
  end

  # POST /stock_loggings
  # POST /stock_loggings.json
  def create
    @stock_logging = StockLogging.new(stock_logging_params)
    respond_to do |format|
    	if @stock_logging.valid?
			#find existing stock
			@stocks = Stock.find_by(drug_id: @stock_logging.drug_id, inventory_id: @stock_logging.inventory_id)
			@drug = Drug.find(@stock_logging.drug_id)
			#check in/out stocks (1 -> in, 2 -> out)
			if (@stock_logging.stock_logging_category_id == 1)
				if @stock_logging.save
					if (@stocks != nil)
						update_stock = @stocks.quantity + @stock_logging.quantity
						@stocks.update(:quantity => update_stock, :inventory_id => @stock_logging.inventory_id)
					else
						@stocks = @drug.stocks.create(:drug_id => @drug.id, :inventory_id => @stock_logging.inventory_id, :quantity => @stock_logging.quantity)	
					end
					@stock_logging.update(:stock_id => @stocks.id, :inventory_id => @stock_logging.inventory_id)
					#create the drug stock_logging links
					@drug.stock_loggings << @stock_logging
					format.html { redirect_to @drug, notice: 'Stock logging was successfully created.' }
					format.json { render :show, status: :created, location: @stock_logging }
				else
					format.html { redirect_to new_drug_stock_logging_path(:drug_id => "transaction"), notice: 'Stock logging processing failed.'  }
					format.json { render json: @stock_logging.errors, status: :unprocessable_entity }
				end
			else
				if (@stocks != nil)
					if (@stocks.quantity >= @stock_logging.quantity)
						if @stock_logging.save
							update_stock = @stocks.quantity - @stock_logging.quantity
							@stocks.update(:quantity => update_stock, :inventory_id => @stock_logging.inventory_id)
							@stock_logging.update(:stock_id => @stocks.id, :inventory_id => @stock_logging.inventory_id)
							#create the drug stock_logging links
							@drug.stock_loggings << @stock_logging
							format.html { redirect_to @drug, notice: 'Stock logging was successfully created.' }
							format.json { render :show, status: :created, location: @stock_logging }
						else
							format.html { redirect_to new_drug_stock_logging_path(:drug_id => "transaction"), notice: 'Stock logging processing failed.' }
							format.json { render json: @stock_logging.errors, status: :unprocessable_entity }
						end
					else
						format.html { redirect_to new_drug_stock_logging_path(:drug_id => "transaction"), notice: 'Stock logging processing failed, unsuficient stock available.' }
						format.json { render json: @stock_logging.errors, status: :unprocessable_entity }
					end
				else
					format.html { redirect_to new_drug_stock_logging_path(:drug_id => "transaction"), notice: 'Stock logging processing failed, unavailable stocks.' }
					format.json { render json: @stock_logging.errors, status: :unprocessable_entity }
				end
			end
		else
			format.html { render :new }
			format.json { render json: @stock_logging.errors, status: :unprocessable_entity }
		end
	end
  end

  # PATCH/PUT /stock_loggings/1
  # PATCH/PUT /stock_loggings/1.json
  def update
    #get previous value of stock_logging before be updated
	@prev_value = StockLogging.find(params[:id])
    respond_to do |format|
      if @stock_logging.update(stock_logging_params)
		
		#find existing stock
		@stocks_ori = Stock.find_by(drug_id: @prev_value.drug_id, inventory_id: @prev_value.inventory_id)
		#Redo the Stock quantity
		#check in/out stocks (1 -> in, 2 -> out)
		if (@prev_value.stock_logging_category_id == 1)
			redo_stock = @stocks_ori.quantity - @prev_value.quantity
		else
			redo_stock = @stocks_ori.quantity + @prev_value.quantity
		end
		@stocks_ori.update(:quantity => redo_stock, :inventory_id => @prev_value.inventory_id)
        
        #update stock with new the value
        #find existing stock
		@stocks = Stock.find_by(drug_id: @stock_logging.drug_id, inventory_id: @stock_logging.inventory_id)
		@drug = Drug.find(@stock_logging.drug_id)
		#check in/out stocks (1 -> in, 2 -> out)
		if (@stock_logging.stock_logging_category_id == 1)
			if @stock_logging.save
				if (@stocks != nil)
					update_stock = @stocks.quantity + @stock_logging.quantity
					@stocks.update(:quantity => update_stock, :inventory_id => @stock_logging.inventory_id)
				else
					@stocks = @drug.stocks.create(:drug_id => @drug.id, :inventory_id => @stock_logging.inventory_id, :quantity => @stock_logging.quantity)	
				end
				@stock_logging.update(:stock_id => @stocks.id, :inventory_id => @stock_logging.inventory_id)
				#create the drug stock_logging links when switch into another drug
				if (@prev_value.drug_id != @stock_logging.drug_id)
					@stock_logging.drugs = Drug.where(:id => @stock_logging.drug_id)
				end
				format.html { redirect_to @drug, notice: 'Stock logging was successfully created.' }
				format.json { render :show, status: :created, location: @stock_logging }
			else
				format.html { redirect_to @drug, notice: 'Stock logging processing failed.'  }
				format.json { render json: @stock_logging.errors, status: :unprocessable_entity }
			end
		else
			if (@stocks != nil)
				if (@stocks.quantity >= @stock_logging.quantity)
					if @stock_logging.save
						update_stock = @stocks.quantity - @stock_logging.quantity
						@stocks.update(:quantity => update_stock, :inventory_id => @stock_logging.inventory_id)
						@stock_logging.update(:stock_id => @stocks.id, :inventory_id => @stock_logging.inventory_id)
						#create the drug stock_logging links when switch into another drug
						if (@prev_value.drug_id != @stock_logging.drug_id)
							@stock_logging.drugs = Drug.where(:id => @stock_logging.drug_id)
						end
						format.html { redirect_to @drug, notice: 'Stock logging was successfully created.' }
						format.json { render :show, status: :created, location: @stock_logging }
					else
						format.html { redirect_to @drug, notice: 'Stock logging processing failed.' }
						format.json { render json: @stock_logging.errors, status: :unprocessable_entity }
					end
				else
					format.html { redirect_to @drug, notice: 'Stock logging processing failed, unsuficient stock available.' }
					format.json { render json: @stock_logging.errors, status: :unprocessable_entity }
				end
			else
				format.html { redirect_to @drug, notice: 'Stock logging processing failed, unavailable stocks.' }
				format.json { render json: @stock_logging.errors, status: :unprocessable_entity }
			end
		end
      else
        format.html { render :edit }
        format.json { render json: @stock_logging.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /stock_loggings/1
  # DELETE /stock_loggings/1.json
  def destroy
    #get previous value of stock_logging before be deleted
	@prev_value = StockLogging.find(params[:id])
	@drug = Drug.find(@prev_value.drug_id)
    respond_to do |format|
		@stock_logging.destroy
		
		#find existing stock
		@stocks_ori = Stock.find_by(drug_id: @prev_value.drug_id, inventory_id: @prev_value.inventory_id)
		#Redo the Stock quantity
		#check in/out stocks (1 -> in, 2 -> out)
		if (@prev_value.stock_logging_category_id == 1)
			redo_stock = @stocks_ori.quantity - @prev_value.quantity
		else
			redo_stock = @stocks_ori.quantity + @prev_value.quantity
		end
		@stocks_ori.update(:quantity => redo_stock, :inventory_id => @prev_value.inventory_id)
	
        format.html { redirect_to @drug, notice: 'Stock logging was successfully destroyed.' }
        format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_stock_logging
      @stock_logging = StockLogging.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def stock_logging_params
      params.require(:stock_logging).permit(:drug_id, :stock_id, :quantity, :inventory_id, :stock_logging_category_id, :created_at, :updated_at, :created_by, :updated_by)
    end
    
    def stock_params
      params.require(:stock).permit(:drug_id, :inventory_id, :quantity, :created_at, :updated_at, :created_by, :updated_by)
    end
end
